package com.zecan.blog.controller;

import com.zecan.blog.common.BlogResult;
import com.zecan.blog.service.QuartzService;
import com.zecan.blog.utils.BlogResultUtils;
import org.quartz.SchedulerException;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Map;

/**
 * @ClassName QuartzController
 * @Description
 * @date 2022/11/15 23:40
 * @Version 1.0
 */
@Controller
@RequestMapping("/quartz")
public class QuartzController {


    @Resource
    QuartzService quartzService;

    @PreAuthorize("hasAuthority('admin')")
    @PostMapping(path = "/addJob")
    @ResponseBody
    public BlogResult addJob(String jName, String jGroup ,String cron,Map<String,Object> map) {
        try {
            quartzService.addArticleGoodJob(jName, jGroup, cron);
            return BlogResultUtils.ok("添加任务成功");
        } catch (Exception e) {
            e.printStackTrace();
            return BlogResultUtils.ok("添加任务失败");

        }
    }

    /**
     * 暂停任务
     *
     * @param jName 任务名称
     * @param jGroup 任务组
     * @return ResultMap
     */
    @PreAuthorize("hasAuthority('admin')")
    @PostMapping(path = "/pauseJob")
    @ResponseBody
    public BlogResult pauseJob(String jName, String jGroup) {
        try {
            quartzService.pauseJob(jName, jGroup);
            return BlogResultUtils.ok("暂停任务成功");
        } catch (SchedulerException e) {
            e.printStackTrace();
            return BlogResultUtils.ok("暂停任务失败");
        }
    }

    /**
     * 恢复任务
     *
     * @param jName 任务名称
     * @param jGroup 任务组
     * @return ResultMap
     */
    @PreAuthorize("hasAuthority('admin')")
    @PostMapping(path = "/resumeJob")
    @ResponseBody
    public BlogResult resumeJob(String jName, String jGroup) {
        try {
            quartzService.resumeJob(jName, jGroup);
            return BlogResultUtils.ok("恢复任务成功");
        } catch (SchedulerException e) {
            e.printStackTrace();
            return BlogResultUtils.ok("恢复任务失败");
        }
    }

    /**
     * 重启任务
     *
     * @param jName 任务名称
     * @param jGroup 任务组
     * @param cron cron表达式
     * @return ResultMap
     */
    @PreAuthorize("hasAuthority('admin')")
    @PostMapping(path = "/rescheduleJob")
    @ResponseBody
    public BlogResult rescheduleJob(String jName, String jGroup, String cron) {
        try {
            quartzService.rescheduleJob(jName, jGroup, cron);
            return BlogResultUtils.ok("重启任务成功");

        } catch (SchedulerException e) {
            e.printStackTrace();
            return BlogResultUtils.ok("重启任务失败");

        }
    }

    /**
     * 删除任务
     *
     * @param jName 任务名称
     * @param jGroup 任务组
     * @return ResultMap
     */
    @PreAuthorize("hasAuthority('admin')")
    @PostMapping(path = "/deleteJob")
    @ResponseBody
    public BlogResult deleteJob(String jName, String jGroup) {
        try {
            quartzService.deleteJob(jName, jGroup);
            return BlogResultUtils.ok("删除任务成功");
        } catch (SchedulerException e) {
            e.printStackTrace();
            return BlogResultUtils.ok("删除任务失败");
        }
    }




}
